package com.hjm.common.utils.yh;


import com.hjm.framework.config.CustomConfig;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


import java.util.Map;

public class GetTmk {
    private static Logger log= LogManager.getLogger(GetTmk.class.getName());

    private LoFunction loFunction = new LoFunction();
    private ProChannelMsg proChannel = new ProChannelMsg();

    public String[] getTmk(Map map) throws Exception{
        ListData listData = new ListData(EncConfig.bitConfig, 0);
        listData.setlist(11, "123456");
        listData.setlist(26, "01");
        listData.setlist(41, map.get("posno")+"");
        listData.setlist(48, loFunction.hexStr2Bytes(map.get("result")+""));
        byte[] body = proChannel.packMessage(listData.getList());
        byte[] msgb = loFunction.getAsciiBytes("H001");
        byte[] dateh = loFunction.shortToBytes((short) (body.length + 4));
        byte[] endBody = loFunction.assemble(dateh, msgb, body);
        BankClient bc = new BankClient();
        byte[] resByte = new byte[0];
        try {
            //TODO TMK秘钥接口
            resByte = bc.connServerT(endBody, CustomConfig.getTradeUrl(), CustomConfig.getTradePort());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("接口异常：" + e);
        }
        PMap<String, String> posInfo = requestInfo(resByte);
        log.info("原生包:" + new String[]{posInfo.get("48"), posInfo.get("61")});

        return new String[]{posInfo.get("48"), posInfo.get("61")};
    }
    public PMap<String, String> requestInfo(byte[] reqByte) throws Exception {
        return parseMessage(reqByte, "3");
    }
    public PMap<String, String> parseMessage(byte[] message, String type)
            throws Exception {
        PMap<String, String> packMap = proChannel.parseMessage(message,EncConfig.bitConfig, 6);
        byte[] lens = new byte[2];
        System.arraycopy(message, 0, lens, 0, 2);
        return packMap;
    }
}
